<?php

namespace Admin\Controller;
use Think\Model;
use Org\Util\Sync;

/**
 * 首页设置控制器
 * @author wangyuanlin
 * 2015-05-17
 */
class HomePageController extends AdminController {

    /**
     * 首页
     * @author wangyuanlin
     * 2015-05-20
     */
    public function index() {
        
        $Model = new Model();
        
        $homePage = M("homepage");
        
        //大海报
        $className1 = 'dhb';
        $list1 = $homePage->where(array('cName'=>$className1))->order('ordnum asc')->select();
        $this->_list1 = $list1;
        
        //小海报
        $className2 = 'xhb';
        $list2 = $homePage->where(array('cName'=>$className2,'status'=>1))->order('ordnum asc')->select();
        $this->_list2 = $list2;
        
        //果然推荐
        $sql3 = "select a.id,a.specId,b.pname,b.name 
            from gr_homepage a,v_product b
            where a.specId = b.id
            and a.cName = 'tuijian'";
        $list3 = $Model -> query($sql3);
        $this->_list3 = $list3;
        
        //果然特价
        $sql4 = "select a.id,a.specId,b.pname,b.name 
            from gr_homepage a,v_product b
            where a.specId = b.id
            and a.cName = 'tejia'";
        $list4 = $Model -> query($sql4);
        $this->_list4 = $list4;
        
        //商品列表
        $sql5 = "select 
            a.id,a.pname,a.name,(select b.fileurl from gr_product_image b where a.imageid = b.id) fileUrl 
            from v_product a 
            where a.status = 1
            order by pname,id desc";
        $prolist = $Model -> query($sql5);
        $this->_prolist = $prolist;
        
        $this->display();
    }

    /**
     * 新增海报
     * @author wangyuanlin
     * 2015-05-17
     */
    public function edit($id = "", $specid = "", $fileUrl = "") {
        
        $data = array('specId' => $specid, 'fileUrl' => $fileUrl, 'lastupDate' => NOW_TIME, 'syncStatus' => 2);
        
        if (!D('homepage')->where(array('id' => $id))->save($data)) {
            $this->error("修改失败！");
        } else {
            $this->success('修改成功！', U('index'));
        }
    }
    
    /**
     * 上推
     * @author wangyuanlin
     * 2015-05-18
     */
    public function push(){
        
        $reuslt = D("HomePage")->push();
        
        if ($reuslt) {
            $this->success('上推成功！', U('index'));
        } else {
            $this->error("上推失败！");
        }
    }

}
